#!/usr/bin/env python

import threading, urllib2, re

class ProxyGrabber(threading.Thread):
    def __init__(self, site, temp_file):
        threading.Thread.__init__(self)
        self.temp_file = temp_file
        self.site = site
        self.errors = {}
    def run(self):
        try:
            read_site = urrlib2.urlopen(self.site).read()
        except ValueError:
            self.errors['ValueError'] = 'Invalid URL type (%s)' % (self.site)
        except urllib2.URLError:
            self.errors['URLError'] = 'Name or service not known (%s)' % (self.site)
        else:
            found_proxies = re.findall('(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\:(?:[\d]{2,5})', read_site)
            for i in found_proxies:
                try:
                    ofile = open('proxies_%d'%(self.temp_file))
                    ofile.write('%s\n'%(i))
                except IOError:
                    self.errors['IOError'] = 'Could not write to %s' % (self.temp_file)
